package com.sqx.modules.app.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.sqx.common.utils.PageUtils;
import com.sqx.modules.app.dao.*;
import com.sqx.modules.app.entity.*;
import com.sqx.modules.app.service.*;
import com.sqx.modules.common.entity.CommonInfo;
import com.sqx.modules.common.service.CommonInfoService;
import com.sqx.modules.pay.entity.PayClassify;
import com.sqx.modules.pay.entity.PayDetails;
import com.sqx.modules.pay.service.PayClassifyService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.math.BigDecimal;
import java.text.SimpleDateFormat;
import java.util.Date;

@Service
@Slf4j
public class UserMoneyServiceImpl extends ServiceImpl<UserMoneyDao, UserMoney> implements UserMoneyService {
    @Autowired
    private UserMoneyDetailsDao userMoneyDetailsDao;
    @Autowired
    private PayClassifyService classifyService;
    @Autowired
    private UserMoneyDetailsService moneyDetailsService;
    @Autowired
    private CommonInfoService commonInfoService;
    @Autowired
    private UserService userService;

    @Override
    public void updateMoney(int i, Long userId, BigDecimal money) {
        baseMapper.updateMayMoney(i, userId, money);
    }


    @Override
    public UserMoney selectUserMoneyByUserId(Long userId) {
        return baseMapper.selectOne(new QueryWrapper<UserMoney>().eq("user_id", userId));
    }

    @Override
    public Double selectMyProfit(Long userId) {

        return userMoneyDetailsDao.selectMyProfit(userId);
    }

    @Override
    public IPage<UserMoneyDetails> balanceDetailed(Long page, Long limit, UserMoneyDetails userMoneyDetails) {
        Page<UserMoneyDetails> pages;
        if (page != null && limit != null) {
            pages = new Page<>(page, limit);
        } else {
            pages = new Page<>();
        }
        return userMoneyDetailsDao.selectPage(pages,new QueryWrapper<>(userMoneyDetails).orderByDesc("create_time"));
    }

    @Override
    public void payCallBack(PayDetails payDetails) {
        PayClassify payClassify = classifyService.getById(payDetails.getRelevanceId());
        BigDecimal money = payClassify.getMoney().add(payClassify.getGiveMoney());
        baseMapper.updateMayMoney(1, payDetails.getUserId(), money);
        UserMoneyDetails userMoneyDetails = new UserMoneyDetails();
        userMoneyDetails.setMoney(payClassify.getMoney());
        userMoneyDetails.setUserId(payDetails.getUserId());
        userMoneyDetails.setTitle("账户充值");
        userMoneyDetails.setType(1);
        userMoneyDetails.setClassify(2);
        SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        userMoneyDetails.setCreateTime(simpleDateFormat.format(new Date()));
        if (payDetails.getClassify() == 4 || payDetails.getClassify() == 5) {
            userMoneyDetails.setContent("支付宝充值：" + payClassify.getMoney() + "积分,赠送积分:" + payClassify.getGiveMoney());
        } else {
            userMoneyDetails.setContent("微信充值：" + payClassify.getMoney() + "积分,赠送积分:" + payClassify.getGiveMoney());
        }
        moneyDetailsService.save(userMoneyDetails);
        CommonInfo commonInfo = commonInfoService.findOne(209);
        UserEntity userEntity = userService.getById(payDetails.getUserId());
        if ("是".equals(commonInfo.getValue())) {
            UserEntity parentEntity = userService.getOne(new QueryWrapper<UserEntity>().eq("invitation_code", userEntity.getInviterCode()));
            //一级分佣
            if (parentEntity != null) {
                if (parentEntity.getZhiRate().compareTo(BigDecimal.ZERO) > 0) {
                    BigDecimal rate = BigDecimal.valueOf(payDetails.getMoney()).multiply(parentEntity.getZhiRate()).setScale(2, BigDecimal.ROUND_HALF_UP);
                    parentEntity.setBalance(parentEntity.getBalance().add(rate));
                    userService.updateById(parentEntity);
                    moneyDetailsService.setParentMoneyDetails(payDetails.getUserId(), parentEntity.getUserId(), rate, 1);
                    UserEntity parentParentEntity = userService.getOne(new QueryWrapper<UserEntity>().eq("invitation_code", parentEntity.getInviterCode()));
                    //二级分佣
                    if (parentParentEntity != null) {
                        if (parentParentEntity.getFeiRate().compareTo(BigDecimal.ZERO) > 0) {
                            rate = BigDecimal.valueOf(payDetails.getMoney()).multiply(parentParentEntity.getFeiRate()).setScale(2, BigDecimal.ROUND_HALF_UP);
                            parentParentEntity.setBalance(parentParentEntity.getBalance().add(rate));
                            userService.updateById(parentParentEntity);
                            moneyDetailsService.setParentMoneyDetails(payDetails.getUserId(), parentParentEntity.getUserId(), rate, 2);
                        }
                    }
                }
            }
        }
    }
}
